package com.ypj.rpc.hook;

import com.ypj.rpc.utils.NacosUtils;
import com.ypj.rpc.factory.ThreadPoolFactory;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadFactory;

@Slf4j
public class ShutDownHook {

//    private final ExecutorService threadPool=ThreadPoolFactory.createDefaultThreadPool("shut_down_hook");
    private static final ShutDownHook SHUT_DOWN_HOOK=new ShutDownHook();

    public static ShutDownHook getShutDownHook(){
        return SHUT_DOWN_HOOK;
    }

    public void addClearAllHook(){
        log.info("服务器关闭后自动注销所有服务");
        Runtime.getRuntime().addShutdownHook(new Thread(()->{
            NacosUtils.clearRegistry();
            ThreadPoolFactory.shutDownAll();
        }));
    }
}
